<template>
  <div class="app-container">
    <div v-if="$permission('stockInconformity:list')">
      <!-- 条件栏 -->
      <div class="filter-wrapper">
        <new-page-filter
          v-model:query="filterInfo.query.queryMap"
          :queryMoreJson="true"
          :filter-list="filterInfo.searchList"
          :filter-btn="filterInfo.searchBtn"
          :slipSelectRequest="slipSelectRequest"
          :clearType="clearType"
        >
        </new-page-filter>
      </div>
      <div class="filter-wrapper">
        <new-page-filter :filter-btn="filterInfo.btnList"> </new-page-filter>
        <div class="display-flex">
          <el-button @click="preferenceInfo.visible = true"
            >偏好设置
          </el-button>
        </div>
      </div>

      <!-- 表格 -->
      <table-container
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :query="filterInfo.query"
        :checkBox="false"
        :tabIndex="true"
        row-key="uuid"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :row-class-name="tableRowClassOtherName"
        @sortChange="sortChange"
        @pageInfo="pageInfo"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-smartCabinetDepotName="scope">
          <span
            :style="
              scope.row.smartCabinetDepotName !== scope.row.stockDepotName
                ? 'color: #ec808d'
                : ''
            "
            >{{ scope.row.smartCabinetDepotName }}</span
          >
        </template>
        <template v-slot:col-stockDepotName="scope">
          <span
            :style="
              scope.row.smartCabinetDepotName !== scope.row.stockDepotName
                ? 'color: #ec808d'
                : ''
            "
            >{{ scope.row.stockDepotName }}</span
          >
        </template>
        <template v-slot:col-smartCabinetQty="scope">
          <span
            :style="
              scope.row.smartCabinetQty !== scope.row.stockQty
                ? 'color: #ec808d'
                : ''
            "
            >{{ $fn.qtyFormatter(scope.row.smartCabinetQty) }}</span
          >
        </template>
        <template v-slot:col-stockQty="scope">
          <span
            :style="
              scope.row.smartCabinetQty !== scope.row.stockQty
                ? 'color: #ec808d'
                : ''
            "
            >{{ $fn.qtyFormatter(scope.row.stockQty) }}</span
          >
        </template>
      </table-container>
    </div>
    <!--偏好 -->
    <set-preference
      v-if="preferenceInfo.visible"
      v-model:visible="preferenceInfo.visible"
      :searchList="filterInfo.searchList"
      :fieldList="tableInfo.fieldList"
      :customizeUserVO="filterInfo.query.customizeUserVO"
      @callback="setPreferenceCallback"
    />

    <advanced-search
      v-if="advancedSearchInfo.visible"
      v-model:visible="advancedSearchInfo.visible"
      v-model:formData="advancedSearchInfo.data"
      :fieldList="tableInfo.fieldList"
      @callback="advancedSearchCallback"
    />
    <OperateAffairDialog
      v-if="operateAffairInfo.visible"
      v-model:showDialog="operateAffairInfo.visible"
      :rfidCode="operateAffairInfo.rfidCode"
    />
  </div>
</template>

<script setup name="stockInconformity">
import { getCustomizeSetting, updateUserCustomize } from '@/api/customize'
import { getDepotAll } from '@/api/core'
import { getStockInconformityCustomizeListPage } from '@/api/smartCabinet'
import OperateAffairDialog from './operateAffairDialog'

const { proxy } = getCurrentInstance()
// 过滤相关配置
const filterInfo = ref({
  sortArr: [],
  query: {
    groupMap: {},
    groups: [],
    sortString: '',
    queryMap: {
      depotAuth: {
        asTbName: 'smart_cabinet_list',
        columnName: 'depot_id',
        queryType: 'IN',
        queryCategory: 'AND',
        value: 'depotAuth',
      },
      deptAuth: {
        asTbName: 'smart_cabinet_list',
        columnName: 'dept_id',
        queryType: 'IN',
        queryCategory: 'AND',
        value: 'deptAuth',
      },
    },
    customizeUserVO: {},
  },
  searchList: [],
  searchBtn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '高级查询', event: advancedSearchShow },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  btnList: [],
})

const clearType = ref(true)
const slipSelectRequest = ref(true)
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  api: undefined,
  selections: [],
  data: [],
  fieldList: [],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '90',
    btList: [
      {
        label: '操作流水对比',
        event: operateAffair,
        show: proxy.$permission('stockInconformity:operateAffair'),
      },
    ],
  },
})

const advancedSearchInfo = ref({
  visible: false,
  data: {},
})
const preferenceInfo = ref({
  visible: false,
})
const operateAffairInfo = ref({
  visible: false,
  rfidCode: '',
})
const page = ref({
  page: true,
  curPage: 1, // 当前页
  pageSize: 20, // 每页条数
})
onBeforeMount(() => {
  let obj = {
    statusValue: {
      label: '状态',
      value: 'statusValue',
      asValue: 'status',
      componentType: 'select-model',
      code: 'new_enabled_type',
      show: false,
      hidden: true,
    },
    typeValue: {
      label: '类型',
      value: 'typeValue',
      asValue: 'type',
      componentType: 'select-model',
      code: 'smart_cabinet_type',
      show: false,
      hidden: true,
    },
    smartCabinetDepotName: {
      label: '实物所在仓库',
      value: 'smartCabinetDepotName',
      asValue: 'smartCabinetDepotId',
      componentType: 'slip-select',
      query: {
        depotAuth: 'id',
        status: 1,
        useTypeId: '1',
      },
      type: 'slot',
      api: getDepotAll, //getDeptAll, getDepotAll, getSupplierAll
      show: false,
      hidden: true,
    },
    stockDepotName: {
      label: '账面所在仓库',
      value: 'stockDepotName',
      asValue: 'stockDepotId',
      componentType: 'slip-select',
      type: 'slot',
      query: {
        depotAuth: 'id',
        status: 1,
        useTypeId: '1',
      },
      api: getDepotAll, //getDeptAll, getDepotAll, getSupplierAll
      show: false,
      hidden: true,
    },
    smartCabinetQty: {
      label: '柜存数量',
      value: 'smartCabinetQty',
      type: 'slot',
      componentType: 'number',
      formatter: proxy.$fn.qtyFormatter,
      show: false,
      hidden: true,
    },
    stockQty: {
      label: '库存数量',
      value: 'stockQty',
      type: 'slot',
      componentType: 'number',
      formatter: proxy.$fn.qtyFormatter,
      show: false,
      hidden: true,
    },
    commonName: {
      label: '通用名称',
      value: 'commonName',
      componentType: 'input',
      show: false,
      hidden: true,
    },
  }
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  filterInfo.value.searchList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  getCustomize()
})
function operateAffair(row) {
  //操作流水对比
  sessionStorage.setItem('storeRowId', row.id)
  operateAffairInfo.value.visible = true
  operateAffairInfo.value.rfidCode = row.rfidCode
}
const pageInfo = (obj) => {
  page.value = obj
}

// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
// 搜索
function search() {
  // 重置分页
  advancedSearchInfo.value.data = {}
  filterInfo.value.query.queryDtoList = []
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}

function tableRowClassOtherName({ row }) {
  let rowId = sessionStorage.getItem('storeRowId')
  if (rowId && row.id === Number(rowId)) {
    return 'check-row'
  }
  return ''
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}

function clearSearch() {
  clearType.value = !clearType.value
  advancedSearchInfo.value.data = {}
  filterInfo.value.query.queryMap = proxy.$fn.deepClone(
    filterInfo.value.copyQueryMap
  )
  search()
}
function getCustomize(slipSelect = true) {
  let param = {
    tableKey:
      'smartCabinetStockInconformityList-com.slip.spd.entity.vo.smartCabinet.SmartCabinetStockInconformityVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = data
      let searchInfo = proxy.$fn.setSearchList(
        filterInfo.value.searchList,
        data.detail
      )
      let tableFieldInfo = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail,
        true
      )
      tableInfo.value.fieldList = tableFieldInfo.fieldList
      filterInfo.value.searchList = searchInfo.searchList
      filterInfo.value.query.queryMap = {
        ...searchInfo.queryMap,
        ...filterInfo.value.query.queryMap,
      }
      filterInfo.value.searchList.forEach((item) => {
        item.event = search
      })
      filterInfo.value.copyQueryMap = proxy.$fn.deepClone(
        filterInfo.value.query.queryMap
      )
      slipSelectRequest.value = slipSelect
      tableInfo.value.api = getStockInconformityCustomizeListPage
      getList()
    }
  })
}

function sortChange(column) {
  if (filterInfo.value.query.customizeUserVO.head.sortType === 0) {
    let arr = proxy.$fn.sortChange(column, filterInfo.value.sortArr)
    filterInfo.value.sortArr = arr
    filterInfo.value.query.sortString = arr.length ? arr.toString() : ''
    getList()
  }
}

async function setPreferenceCallback(obj) {
  updateUserCustomize(obj.customizeUserVO).then((res) => {
    if (res && res.success) {
      getCustomize(!slipSelectRequest.value)
    }
  })
}
function advancedSearchShow() {
  advancedSearchInfo.value.visible = true
}

async function advancedSearchCallback(obj) {
  let arr = [
    {
      asTbName: 'smart_cabinet_list',
      columnName: 'depot_id',
      queryType: 'IN',
      queryCategory: 'AND',
      value: 'depotAuth',
    },
    {
      asTbName: 'smart_cabinet_list',
      columnName: 'dept_id',
      queryType: 'IN',
      queryCategory: 'AND',
      value: 'deptAuth',
    },
  ]
  filterInfo.value.query.queryMap = {}
  filterInfo.value.query.queryDtoList = [...obj.queryDtoList, ...arr]
  advancedSearchInfo.value.data = obj.data
  await getList()
  clearType.value = !clearType.value
  filterInfo.value.query.queryMap = proxy.$fn.deepClone(
    filterInfo.value.copyQueryMap
  )
}
</script>

<style scoped></style>
